package com.sl.carcode.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
import com.sl.carcode.R;
import com.sl.carcode.adapter.GoodLVAdapter;
import com.sl.carcode.bean.GoodInfo;
import com.sl.carcode.utils.GsonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ShopActivity extends AppCompatActivity {
    private Map<String, String> exParams = new HashMap();
    private GoodLVAdapter goodLVAdapter;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
//        login();
        initView();

    }

    private ArrayList<GoodInfo> list = new ArrayList();

    @Override
    protected void onStart() {
        super.onStart();
        OkHttpClient okHttpClient = new OkHttpClient();
//        String url = "http://api.dataoke.com/index.php?r=goodsLink/www";
//        url += "&type=www_quan" + "&appkey=7534a6b097" + "&v=2&page=20";
        String url = "http://api.dataoke.com/index.php?r=Port/index";
        url += "&type=top100" + "&appkey=7534a6b097" + "&v=2&page=10";
        Request request = new Request.Builder().url(url).get().build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        JSONObject jo = jsonObject.getJSONObject("data");
                        Log.i("sl", "data格式" + jo.toString());
                        JSONArray jsonArray = jsonObject.getJSONArray("result");
                        Log.i("sl", "result格式" + jsonArray.toString());
                        ArrayList<GoodInfo> arrayListlist = GsonUtils.jsonArray2List(jsonArray.toString(), GoodInfo.class);
                        list = arrayListlist;
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        goodLVAdapter = new GoodLVAdapter(ShopActivity.this, list);
                        lv.setAdapter(goodLVAdapter);
                    }
                });
            }
        });
    }

    private void ShowDialog(final Intent intent) {
        android.app.AlertDialog.Builder ab = new android.app.AlertDialog.Builder(this);
        ab.setTitle("系统提示").setMessage("将前往淘宝").setNeutralButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                login(lv, intent);
            }
        }).create().show();
    }

    public void login(View view, final Intent intent) {
        final AlibcLogin alibcLogin = AlibcLogin.getInstance();

        alibcLogin.showLogin(new AlibcLoginCallback() {

            @Override
            public void onSuccess(int i) {
                Toast.makeText(ShopActivity.this, "登录成功 ",
                        Toast.LENGTH_LONG).show();
                startActivity(intent);
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(ShopActivity.this, "登录失败 ",
                        Toast.LENGTH_LONG).show();
            }
        });
    }

    public void login() {
        final AlibcLogin alibcLogin = AlibcLogin.getInstance();

        alibcLogin.showLogin(new AlibcLoginCallback() {

            @Override
            public void onSuccess(int i) {
                Toast.makeText(ShopActivity.this, "登录成功 ",
                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(ShopActivity.this, "登录失败 ",
                        Toast.LENGTH_LONG).show();
            }
        });
    }


    private void initView() {
        lv = (ListView) findViewById(R.id.lv);
        goodLVAdapter = new GoodLVAdapter(this, list);
        lv.setAdapter(goodLVAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(ShopActivity.this, DetailActivity.class);
                intent.putExtra("Good", list.get(i));
                ShowDialog(intent);
            }
        });
        //http://api.dataoke.com/index.php?r=goodsLink/www&type=www_quan&appkey=您申请的appkey&v=xxx&page=xxx
//        Map<String, String> exParams = new HashMap<>();
//        exParams.put(AlibcConstants.ISV_CODE, "appisvcode");

        //商品详情page
//        AlibcBasePage detailPage = new AlibcDetailPage(itemId);

        //实例化店铺打开page
        //AlibcBasePage shopPage = new AlibcShopPage(shopId);

        //实例化添加购物车打开page
        //AlibcBasePage addCardPage = new AlibcAddCartPage(itemId);

        //实例化我的订单打开page
        //AlibcBasePage ordersPage = new AlibcMyOrdersPage(status, allOrder);

        //实例化我的购物车打开page
//        AlibcBasePage myCartsPage = new AlibcMyCartsPage();

        //实例化URL打开page
//        AlibcBasePage page = new AlibcPage(taokeUrl);

        //设置页面打开方式
//        AlibcShowParams showParams = new AlibcShowParams(OpenType.Native, false);
//        WebView webView = (WebView) findViewById(R.id.web);
////      AlibcTaokeParams alibcTaokeParams = new AlibcTaokeParams("mm_128779697_0_0", "mm_128779697_0_0", "mm_128779697_0_0"); // 若非淘客taokeParams设置为null即可
//        AlibcTaokeParams alibcTaokeParams = new AlibcTaokeParams(); // 若非淘客taokeParams设置为null即可
//        alibcTaokeParams.pid = "mm_128779697_0_0";
//        alibcTaokeParams.subPid = "mm_128779697_0_0";
//        AlibcBasePage alibcBasePage = new AlibcDetailPage("532128520567");
//        AlibcShowParams alibcShowParams = new AlibcShowParams(OpenType.H5, false);
//        exParams.put("isv_code", "appisvcode");
//        exParams.put("alibaba", "阿里巴巴");
//        AlibcTrade.show(ShopActivity.this, webView, null, null, alibcBasePage, alibcShowParams, alibcTaokeParams, exParams, new DemoTradeCallback());
        //获取淘宝用户信息
//        AlibcTrade.show(this, webView, webViewClient, webChromeClient, tradePage, showParams, taokeParams, trackParam, new AlibcTradeCallback() {
//
//            @Override
//            public void onTradeSuccess(AlibcTradeResult alibcTradeResult) {
//
//            }
//
//            @Override
//            public void onFailure(int code, String msg) {
//                //打开电商组件，用户操作中错误信息回调。code：错误码；msg：错误信息
//            }
//        });
    }
}
